package com.sse.casa;

import java.util.ArrayList;
import java.util.List;

import com.sse.casa.simulacion.ConectorBD;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Deployer.java
 *
 * Created on 26/06/2011, 02:19:41 PM
 */

/**
 * 
 * @author Paul
 */
public class Deployer extends javax.swing.JFrame {

	private static final long serialVersionUID = 7318548853938930454L;

	/** Creates new form Deployer */
	public Deployer() {
		initComponents();
		inicializacionLocal();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel10 = new javax.swing.JLabel();
		textBDHost = new javax.swing.JTextField();
		jLabel9 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		textBDNombre = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		textBDUsuario = new javax.swing.JTextField();
		textBDPassword = new javax.swing.JPasswordField();
		jPanel2 = new javax.swing.JPanel();
		jLabel6 = new javax.swing.JLabel();
		textCasaFinal = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		textCasaInicial = new javax.swing.JTextField();
		textIPServidor = new javax.swing.JTextField();
		textPuertoServidor = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		botonCargarInformacion = new javax.swing.JButton();
		botonIniciar = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Simulador de Inmuebles y Sensores");

		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Base De Datos Origen"));

		jLabel10.setText("Password:");

		jLabel9.setText("Usuario:");

		jLabel8.setText("BD Nombre:");

		jLabel7.setText("BD host:");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(jLabel8)
														.addComponent(jLabel7)
														.addComponent(jLabel9)
														.addComponent(jLabel10))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																textBDPassword)
														.addComponent(
																textBDNombre)
														.addComponent(
																textBDUsuario)
														.addComponent(
																textBDHost,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																162,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(34, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel7)
														.addComponent(
																textBDHost,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																textBDNombre,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel8))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel9)
														.addComponent(
																textBDUsuario,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel10)
														.addComponent(
																textBDPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(48, Short.MAX_VALUE)));

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Inializacion de Inmuebles"));

		jLabel6.setText("Final:");

		jLabel5.setText("Inicial:");

		jLabel2.setText("IP Sistema Central:");

		jLabel3.setText("Puerto Sistema Central:");

		jLabel4.setText("Rango de Inmuebles:");

		botonCargarInformacion.setText("Cargar Informacion");
		botonCargarInformacion
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						botonCargarInformacionActionPerformed(evt);
					}
				});

		botonIniciar.setText("Iniciar");
		botonIniciar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				botonIniciarActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jLabel3))
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								jPanel2Layout
																										.createSequentialGroup()
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addGroup(
																												jPanel2Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																textIPServidor,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																191,
																																Short.MAX_VALUE)
																														.addComponent(
																																textPuertoServidor,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																191,
																																Short.MAX_VALUE)))
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								jPanel2Layout
																										.createSequentialGroup()
																										.addGap(21,
																												21,
																												21)
																										.addGroup(
																												jPanel2Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING)
																														.addComponent(
																																jLabel5)
																														.addComponent(
																																jLabel6))
																										.addGap(18,
																												18,
																												18)
																										.addGroup(
																												jPanel2Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																textCasaFinal,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																131,
																																Short.MAX_VALUE)
																														.addComponent(
																																textCasaInicial,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																131,
																																Short.MAX_VALUE)
																														.addComponent(
																																botonIniciar,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																80,
																																javax.swing.GroupLayout.PREFERRED_SIZE)))))
														.addComponent(
																botonCargarInformacion))
										.addContainerGap()));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																textIPServidor,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																textPuertoServidor,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel3))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																textCasaInicial,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel4)
														.addComponent(jLabel5))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																textCasaFinal,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6))
										.addGap(12, 12, 12)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																botonCargarInformacion)
														.addComponent(
																botonIniciar))
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGap(18, 18, 18)
						.addComponent(jPanel2,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addComponent(jPanel1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(25, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(29, 29, 29)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														jPanel1,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jPanel2,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap(22, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	private void botonIniciarActionPerformed(java.awt.event.ActionEvent evt) {
		iniciar();
	}

	private void botonCargarInformacionActionPerformed(
			java.awt.event.ActionEvent evt) {
		cargarInformacion();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {

		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Deployer().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton botonCargarInformacion;
	private javax.swing.JButton botonIniciar;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JTextField textBDHost;
	private javax.swing.JTextField textBDNombre;
	private javax.swing.JPasswordField textBDPassword;
	private javax.swing.JTextField textBDUsuario;
	private javax.swing.JTextField textCasaFinal;
	private javax.swing.JTextField textCasaInicial;
	private javax.swing.JTextField textIPServidor;
	private javax.swing.JTextField textPuertoServidor;
	// End of variables declaration

	private String ipServidor;
	private int puertoServidor;
	private int rangoInicial;
	private int rangoFinal;

	private String bdServidor;
	private String bdNombre;
	private String bdUsuario;
	private String bdPassword;

	private List<CasaInteligenteApp> casas = new ArrayList<CasaInteligenteApp>();

	/**
	 * 
	 */
	private void cargarInformacion() {

		this.ipServidor = textIPServidor.getText();
		this.puertoServidor = Integer.parseInt(textPuertoServidor.getText());

		this.rangoInicial = Integer.parseInt(textCasaInicial.getText());
		this.rangoFinal = Integer.parseInt(textCasaFinal.getText());

		this.bdServidor = textBDHost.getText();
		this.bdUsuario = textBDUsuario.getText();
		this.bdNombre = textBDNombre.getText();
		this.bdPassword = String.valueOf(textBDPassword.getPassword());

		ConectorBD.setProperties(this.bdServidor, this.bdNombre,
				this.bdUsuario, this.bdPassword);

		for (int i = rangoInicial; i <= rangoFinal; i++) {

			try {
				CasaInteligenteApp casa = new CasaInteligenteApp(i);
				casa.setSistemaCentralHost(this.ipServidor);
				casa.setSistemaCentrarlPuerto(puertoServidor);
				casa.inicializarComunicacion();

				this.casas.add(casa);
				// casa.iniciar();
				
			} catch (Exception e) {
				//System.out.print("Error al iniciar la casa " + i);
				e.printStackTrace();
			}

		}

	}

	private void iniciar() {

		for (CasaInteligenteApp c : this.casas) {
			c.iniciar();
		}

	}

	private void inicializacionLocal() {
		this.setLocationRelativeTo(null);

		this.textBDNombre.setText("ash");
		this.textBDHost.setText("localhost");
		this.textBDUsuario.setText("postgres");
		this.textBDPassword.setText("postgres");

		this.textIPServidor.setText("localhost");
		this.textPuertoServidor.setText("5001");
		this.textCasaFinal.setText("1");
		this.textCasaInicial.setText("1");

	}
	
}

	
